//
//  ConfirmCouponCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/2.
//

import UIKit
import RxSwift
//typealias ConfirmCouponCellBlock = (String)->Void
protocol ConfirmCouponCellDelegate: NSObjectProtocol {
    
    /// 输入框信息
    func didChangeCouponCellInputValue(txt: String)
    
    ///tag  0 优惠码  1 优惠券
    func didChangeCouponCellType(tag: NSInteger)
}
class ConfirmCouponCell: UITableViewCell {
    var couponList: [CouponDataModel] = []
    var selectIndex: NSInteger = 0

    private var codeData: CouponDataModel?
    private var dis = DisposeBag()
    var delegate: ConfirmCouponCellDelegate?
//    public var inputBlock: ConfirmCouponCellBlock?
    private var bkgView: UIView = {
        let tempView = UIView()
        tempView.layer.masksToBounds = true
        tempView.layer.cornerRadius = 8
        tempView.backgroundColor = .white
        return tempView
    }()
    private var codeBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.setup_titleLabel(text: MMLocalizedString("confirm_coupon_code", comment: "优惠码"))
        tempBtn.setup_titleColor(color: Theme.titleLabColor)
        tempBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        return tempBtn
    }()
    
    private var couponBtn: UIButton = {
        let tempBtn = UIButton()
        tempBtn.setup_titleLabel(text: MMLocalizedString("confirm_coupon", comment: "优惠券"))
        tempBtn.setup_titleColor(color: Theme.titleLabColor)
        tempBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        return tempBtn
    }()
    
    private var arrowImgView: UIImageView = {
        let tempImgView = UIImageView()
        tempImgView.contentMode = .scaleAspectFit
        tempImgView.image = UIImage.init(named: "navi_select")
        return tempImgView
    }()
    
    private var scrollView: UIScrollView = {
        let tempView = UIScrollView()
        tempView.isScrollEnabled = false
        tempView.showsHorizontalScrollIndicator = false
        return tempView
    }()
    
    private var codeView: ConfirmCodeView!
    private var couponView: ConfirmCouponView!
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = Theme.viewBkgGrayColor
        
        contentView.addSubview(bkgView)
        bkgView.addSubview(codeBtn)
        bkgView.addSubview(couponBtn)
        bkgView.addSubview(arrowImgView)
        bkgView.addSubview(scrollView)
        
        codeBtn.addTarget(self, action: #selector(codeBtnAction), for: .touchUpInside)
        couponBtn.addTarget(self, action: #selector(couponBtnAction), for: .touchUpInside)
        
        let tempWidth: CGFloat = kScreenWidth - 28
        scrollView.contentSize = CGSize.init(width: tempWidth * 2, height: 171)
        
        //优惠码兑换view
        codeView = ConfirmCodeView.init(frame: CGRect.init(x: 0, y: 0, width: tempWidth, height: 116))
        codeView.inputTF.rx.controlEvent(.editingChanged).subscribe {[weak self] _ in
            if let txt = self?.codeView.inputTF.text, txt.count >= 6 {
                //self?.inputBlock?(txt)
                self?.delegate?.didChangeCouponCellInputValue(txt: txt)
            }
        }.disposed(by: self.dis)
        scrollView.addSubview(codeView)
        
        //优惠券列表view
        couponView = ConfirmCouponView.init(frame: CGRect.init(x: tempWidth, y: 0, width: tempWidth, height: 116))
        scrollView.addSubview(couponView)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        bkgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.top.equalTo(10)
            make.bottom.equalTo(0).priority(.high)
        }
        
        codeBtn.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.height.equalTo(34)
            make.top.equalTo(10)
            make.width.equalTo(bkgView.snp.width).dividedBy(2)
        }
        
        couponBtn.snp.makeConstraints { make in
            make.left.equalTo(codeBtn.snp.right)
            make.height.equalTo(34)
            make.top.equalTo(10)
            make.right.equalTo(0)
        }
        
        arrowImgView.snp.makeConstraints { make in
            make.width.equalTo(20)
            make.height.equalTo(8)
            make.centerX.equalTo((kScreenWidth-28)/4)
            make.centerY.equalTo(44)
        }
        
        
        scrollView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.width.equalTo(kScreenWidth-28)
            make.height.greaterThanOrEqualTo(171)
            make.top.equalTo(codeBtn.snp.bottom).offset(10)
            make.bottom.equalTo(0).priority(.high)
        }
        
        codeView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
            make.width.equalTo(scrollView.snp.width)
        }
        
        couponView.snp.makeConstraints { make in
            make.left.equalTo(codeView.snp.right)
            make.top.equalTo(0)
            make.bottom.lessThanOrEqualTo(0)
            make.width.equalTo(scrollView.snp.width)
        }
        
        codeView.layoutIfNeeded()
        let codeView_height = codeView.frame.size.height
        scrollView.snp.updateConstraints { make in
            make.height.greaterThanOrEqualTo(codeView_height)
        }
        self.layoutIfNeeded()
    }
    
    ///优惠码
    @objc func codeBtnAction(){
        self.selectIndex = 0
        self.codeBtn.setup_titleColor(color: Theme.mainColor)
        self.couponBtn.setup_titleColor(color: Theme.titleLabColor)
        self.arrowImgView.snp.updateConstraints { make in
            make.centerX.equalTo((kScreenWidth-28)/4)
        }
        self.resetLayout(tag: 0)
        self.delegate?.didChangeCouponCellType(tag: 0)

    }
    
    ///优惠券
    @objc func couponBtnAction(){
        self.selectIndex = 1
        self.codeBtn.setup_titleColor(color: Theme.titleLabColor)
        self.couponBtn.setup_titleColor(color: Theme.mainColor)
        self.arrowImgView.snp.updateConstraints { make in
            make.centerX.equalTo((kScreenWidth-28) * 3/4)
        }
        self.resetLayout(tag: 1)
        self.delegate?.didChangeCouponCellType(tag: 1)
    }
    
    private func resetLayout(tag: NSInteger){
        codeView.layoutIfNeeded()
        couponView.layoutIfNeeded()
        var scroll_height = codeView.frame.size.height
        if tag == 1 {
            scroll_height = couponView.collectionView.frame.maxY + 20
        }
        self.scrollView.snp.updateConstraints { make in
            make.height.greaterThanOrEqualTo(scroll_height)
        }
        
        self.updateConstraints()
        if let table = self.superview as? UITableView {
            table.beginUpdates()
            table.endUpdates()
        }
        UIView.animate(withDuration: 0.2) {
            self.scrollView.contentOffset.x = CGFloat(self.selectIndex) * self.scrollView.frame.size.width
        }
    }
    
    ///赋值 data: 兑换data
    func setConfirmCouponCell(data: CouponDataModel? = nil, msg: String) {
        self.codeData = data
        if let tempData = data {
            self.codeView.descLab1.text = tempData.giftTypeStr ?? ""
            self.codeView.signLab.text = XTManageCenter.getCouponuByscene(useScene: tempData.useScene ?? "")
        }else{
            self.codeView.signLab.text = ""
        }
        
        //兑换码
        self.codeView.statusLab.text = msg
        self.codeView.reloadLabelUpdate()
        self.codeView.layoutIfNeeded()
        
        //优惠券列表
        self.couponView.setCouponData(coupons: self.couponList)
        self.couponView.layoutIfNeeded()
        
        let codeView_height = codeView.frame.size.height
        let couponList_height = couponView.frame.size.height
        scrollView.snp.updateConstraints { make in
            make.height.greaterThanOrEqualTo(max(codeView_height, couponList_height))
        }
        self.resetLayout(tag: self.selectIndex)
        self.layoutIfNeeded()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
